﻿<?php
class Longadmin_ProductController extends Zend_Controller_Action
{
	// them sản phẩm
	public function addAction()
	{
		$form = new Form_ProductForm();
		$form ->setAction('add')
			  ->setMethod('post');
			  
		if($form->image->receive()){
			$_IMAGE = $form->image->getFileName(null,false); 
		}
		$this->view->message = $form;
		if($this->_request->isPost()){
			$productModel = new Model_Product();
			$result = $productModel->createProduct ($_POST['cat_id'],$_POST['s_id'],$_POST['p_ten'],$_POST['p_gia'],$_POST['p_soLuong'],0,$_IMAGE);
			$this->view->message = "Đã hoàn thành sản phẩm .". $_POST['p_ten'].
			"<br /> <a href='http://shoppingcartcnpm.orgfree.com/index.php/Longadmin/admin/adddetail?cat_id=".$_POST['cat_id']."&p_id=".$result['p_id']."'>Hoàn thiện chi tiết sản phẩm</a>" ;
		}			  
	}
	
	public function adddetailAction()
	{

		if($this->_request->isPost()){
			session_start();
			$productDetailsModel = new Model_ProductDetails();
			$catdesModel = new Model_Catdes();
			$catdesArray = $catdesModel->getCatdes($_SESSION['cat_id']);
			foreach($catdesArray as $x)
			{
				$productDetailsModel->createProductDetails($_SESSION['p_id'],$x['catdes_id'],$_POST[$x['catdes_id']]);
			}

			//echo "SESSION[cat_id] bkgbkgk". $_SESSION['cat_id'] ."<br />";
			//echo "SESSION[p_id]gfgogo". $_SESSION['p_id'] ."<br />";
			$this->view->message = 'Đã hoàn thành.';			
		}
		else
		{
			$form = new Form_PDetailsForm();
			$form->setAction('adddetail')
				 ->setMethod('post');
			$form->setForm($_GET['cat_id']);


			/*
			$form->addElement('text','cat_id',array(
					'required'   => true,
					'label'      =>'CatID:',
					'hidden' => true,
					'value' =>
				));
				*/
			session_start();
			$_SESSION['cat_id'] = $_GET['cat_id'];
			$_SESSION['p_id'] = $_GET['p_id'];
			//
			$this->view->message = $form;
			//echo "SESSION[cat_id]". $_SESSION['cat_id'] ."<br />";
			//echo "SESSION[p_id]". $_SESSION['p_id'] ."<br />";
		
		
		}
	}
	
	public function updateAction()
	{
		$form = new Form_ProductForm();
		$form ->setAction('update')
			  ->setMethod('post');
		$this->view->form = $form;
	}

	public function showAction()
	{
		$rp = $_POST['rp'];
		$page = $_POST['page'];
		$pageStart = ($page-1)*$rp;
		$modelProduct = new Model_Product();
		$totalarray = $modelProduct->getProductAll();
		$result= $modelProduct->getProductLimit($rp,$pageStart);	
		$test = array();
		$i=0;
		foreach($result as $x)
		{
			$test[$i]['p_id'] = "".$x['p_id']."";
			$modelCat = new Model_Category();
			$resultCat = $modelCat->getCategoryByID($x['cat_id']);
			$test[$i]['cat_id'] = "".$resultCat[0]['cat_ten']."";
			$modelSup = new Model_Supplier();
			$resultSup = $modelSup->getSupplierById($x['s_id']);
			$test[$i]['s_id'] = "".$resultSup[0]['s_ten']."";
			$test[$i]['p_ten'] = "".$x['p_ten']."";
			$test[$i]['p_gia'] = "".$x['p_gia']."";
			$test[$i]['p_soLuong'] = "".$x['p_soLuong']."";		
			$test[$i]['p_daBan'] = "".$x['p_daBan']."";
			$test[$i]['p_linkImg'] = "<input type='image' src='/public/images/product/"	.$x['p_linkImg']. "' width='40' height='40'/>";
			$test[$i]['p_khuyenmai'] = "".$x['p_khuyenmai']."";
			$i++;
		}
		
		$data = array();
		$data['page'] = $_POST['page'];
		$data['total'] = count($totalarray);
		foreach ($test as $x) {
			$data['rows'][] = array(
			'id' => $x['p_id'],
			'cell' => array($x['p_id'],$x['cat_id'], $x['s_id'],$x['p_ten'],$x['p_gia'], $x['p_soLuong'],$x['p_daBan'], $x['p_linkImg'], $x['p_khuyenmai']));
		}
		
		$this->_helper->viewRenderer->setNoRender();
		echo json_encode($data);	
	
	}
	
	public function addauctionAction()
	{
		if($this->_request->isPost())
		{	
			session_start();
			if($_SESSION['p_id']>0){
				$p_id =$_SESSION['p_id'];		
				$pa_giobatdau = $_POST['pa_giobatdau'];
				$pa_gioketthuc = $_POST['pa_gioketthuc'];
				$pa_giakhoidiem = $_POST['pa_giakhoidiem'];
				$pa_buocgia = $_POST['pa_buocgia'];
				$pa_tinhtrang = $_POST['pa_tinhtrang'];
				$modelAuction = new Model_ProductAuction();
				$modelAuction->createProductAuction($p_id,$pa_giakhoidiem,$pa_buocgia,null,$pa_giobatdau,$pa_gioketthuc,null,$pa_tinhtrang);
				$this->view->form = 'Đã tạo xong sản phẩm đấu giá với pid = '.$p_id;
			}
			else{
				$this->view->form = 'Bạn chưa chọn sản phẩm';
			}
		}
		else
		{
			$p_id = $_GET['id'];
			session_start();
			$_SESSION['p_id'] = $p_id;
			$form = new Form_AuctionForm();
			$this->view->form = $form;
		}
	}
	
	public function deleteauctionAction()
	{
			$pa_id = $_GET['id'];
			$model_ProductAuction = new Model_ProductAuction();
			$model_ProductAuction->deleteAuctionById($pa_id);	
	}	
	
	public function showauctionAction()
	{
		$rp = $_POST['rp'];
		$page = $_POST['page'];
		$pageStart = ($page-1)*$rp;
		$modelProductAuction = new Model_ProductAuction();

		$totalarray = $modelProductAuction->getProductAuctionAll();
		$result= $modelProductAuction->getProductAuctionLimit($rp,$pageStart);	

		$test = array();
		$i=0;
		foreach($result as $x)
		{
			$test[$i]['pa_id'] = "".$x['pa_id']."";
			$test[$i]['p_id'] = "".$x['p_id']."";
			$test[$i]['pa_giakhoidiem'] = "".$x['pa_giakhoidiem']."";
			$test[$i]['pa_buocgia'] = "".$x['pa_buocgia']."";
			if($x['u_id']!=null){
				$modelUser = new Model_User();
				$winUser = $modelUser->getUserById($x['u_id']);
				$test[$i]['u_id'] = "".$winUser[0]['u_hoten']."";
			}
			else{
				$test[$i]['u_id'] = "Không";
			}
			$test[$i]['pa_giobatdau'] = "".$x['pa_giobatdau']."";
			$test[$i]['pa_gioketthuc'] = "".$x['pa_gioketthuc']."";		
			$test[$i]['pa_giaketthuc'] = "".$x['pa_giaketthuc']."";
			$test[$i]['pa_tinhtrang'] = "".$x['pa_tinhtrang']."";
			$i++;
		}
		
		$data = array();
		$data['page'] = $page;
		$data['total'] = count($totalarray);
		foreach ($test as $x) {
			$data['rows'][] = array(
			'id' => $x['pa_id'],
			'cell' => array($x['pa_id'],$x['p_id'],$x['pa_giakhoidiem'], $x['pa_buocgia'],$x['u_id'],
						$x['pa_giobatdau'], $x['pa_gioketthuc'],$x['pa_giaketthuc'], $x['pa_tinhtrang']
						)
			);
		}
		
		$this->_helper->viewRenderer->setNoRender();
		echo json_encode($data);		
	}
	
}